package GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import Xuly.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JSeparator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JDesktopPane;

public class CreateTableDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtTableName;
	private JTable jTable;
	private ArrayList<MyColumn> columns = new ArrayList<MyColumn>();
	private DefaultTableModel model = new DefaultTableModel();
	public MyTable myTable = new MyTable();
	private String iColumn = "";
	private MyDatabase database;
	private Point pointThis;
	private int rowIndex;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CreateTableDialog dialog = new CreateTableDialog(null, new Point(
					400, 200));
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CreateTableDialog(MyDatabase database, Point point) {

		this.database = database;

		setModalityType(ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setModal(true);
		setTitle("Create table" + " - Database: " + database.getName());
		setBounds(point.x, point.y, 458, 351);
		pointThis = getLocation();

		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblTableName = new JLabel("Table name: ");
			lblTableName.setBounds(10, 14, 92, 14);
			contentPanel.add(lblTableName);
		}
		{
			txtTableName = new JTextField();
			txtTableName.setBounds(102, 11, 231, 20);
			contentPanel.add(txtTableName);
			txtTableName.setColumns(10);
		}

		final JButton btnRemove = new JButton("Remove");
		final JButton btnEdit = new JButton("Edit");
		final JButton btnAddColumn = new JButton("Add column");
		btnEdit.setEnabled(false);
		btnRemove.setEnabled(false);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 40, 430, 1);
		contentPanel.add(separator);
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {

				}
			});
			scrollPane.setBounds(10, 52, 323, 185);
			contentPanel.add(scrollPane);
			{
				jTable = new JTable();
				jTable.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {

						rowIndex = jTable.getSelectedRow();
						iColumn = jTable.getModel().getValueAt(rowIndex, 0)
								.toString();

						if (iColumn != "") {
							btnEdit.setEnabled(true);
							btnRemove.setEnabled(true);
						}

					}
				});

				scrollPane.setViewportView(jTable);
				model = new DefaultTableModel(new Object[][] {}, new String[] {
						"Column name", "Value", "Type value" }) {
					Class[] columnTypes = new Class[] { String.class,
							Object.class, String.class };

					public Class getColumnClass(int columnIndex) {
						return columnTypes[columnIndex];
					}
				};
				jTable.setModel(model);
			}
		}
		{

			btnAddColumn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					CreateColumnDialog createColumnDialog = new CreateColumnDialog(
							myTable, new Point(pointThis.x + 50,
									pointThis.y + 50));
					createColumnDialog.setVisible(true);

					MyColumn newColumn = createColumnDialog.myColumn;
					if (newColumn != null) {

						columns.add(newColumn);
						String[] newTableColumn = { newColumn.getName().toString(),
								newColumn.getType() };
						model.addRow(newTableColumn);
					}
				}
			});

			btnAddColumn.setBounds(339, 52, 101, 23);
			contentPanel.add(btnAddColumn);
		}

		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				CreateColumnDialog createColumnDialog = new CreateColumnDialog(
						myTable, new Point(pointThis.x + 50, pointThis.y + 50));
				String value = jTable.getModel().getValueAt(rowIndex, 1)
						.toString();
				String typeValue = jTable.getModel().getValueAt(rowIndex, 1)
						.toString();
				MyColumn column = new MyColumn(iColumn, typeValue);
				createColumnDialog.myColumn = column;

				createColumnDialog.flagEdit = iColumn;
				createColumnDialog.setVisible(true);

				column = createColumnDialog.myColumn;
				if (column == null)
					return;
				else {
					for (int i = 0; i < columns.size(); i++)
						if (columns.get(i).getName().equals(iColumn))
							columns.remove(i);
					columns.add(column);

					jTable.getModel().setValueAt(column.getName(), rowIndex, 0);
				//	jTable.getModel().setValueAt(column.getValue().toString(), rowIndex, 1);
					jTable.getModel().setValueAt(column.getType(), rowIndex, 2);
				}

			}
		});

		btnEdit.setBounds(339, 81, 101, 23);
		contentPanel.add(btnEdit);

		btnRemove.setBounds(339, 112, 101, 23);
		contentPanel.add(btnRemove);

		JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBounds(333, 129, 1, 1);
		contentPanel.add(desktopPane);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						if (txtTableName.getText().equals("")) {
							MessageDialog messageDialog = new MessageDialog(
									"Nhập table name!", new Point(
											pointThis.x + 50, pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (txtTableName.getText().contains(" ")) {
							MessageDialog messageDialog = new MessageDialog(
									"Table name không được có khoảng trắng. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (ktLaSo(txtTableName.getText().toString())) {
							MessageDialog messageDialog = new MessageDialog(
									"Table name có chứa kí tự số. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						if (ktTrungTableName()) {
							MessageDialog messageDialog = new MessageDialog(
									"Table name bị trùng. Nhập lại!",
									new Point(pointThis.x + 50,
											pointThis.y + 50));
							messageDialog.setVisible(true);
							return;
						}
						
						myTable = new MyTable(
								txtTableName.getText().toString(), columns);
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						myTable = null;
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public Boolean ktTrungTableName() {
		for (int i = 0; i < database.getTables().size(); i++)
			if (database.getTables().get(i).getName()
					.equals(txtTableName.getText().toString()))
				return true;
		return false;
	}

	Boolean ktLaSo(String st) {
		if (st.contains("1"))
			return true;
		if (st.contains("2"))
			return true;
		if (st.contains("3"))
			return true;
		if (st.contains("4"))
			return true;
		if (st.contains("5"))
			return true;
		if (st.contains("6"))
			return true;
		if (st.contains("7"))
			return true;
		if (st.contains("8"))
			return true;
		if (st.contains("9"))
			return true;

		if (st.contains("0"))
			return true;

		return false;
	}
}
